<template>
  <div class="app-container">
    <div class="tab">
      <div
        class="tab-item"
        :class="isActive == index ? 'active' : ''"
        v-for="(item, index) in tabs"
        @click="handleTab(index, item)"
      >
        {{ item }}
      </div>
    </div>
    <div
      class="header"
      style="display: flex; justify-content: space-between"
    >
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item>
          <el-button
            icon="refresh"
            color=" rgb(69, 76, 105)"
            style="color: #fff"
            @click="resetQuery()"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            @click="handleExport"
            color="rgb(146,226,206)"
            style="color: #fff; padding: 0 20px; text-shadow: 0 0 4px #32003c"
            v-hasPermi="['system:set:import']"
          >
            导 出
          </el-button>
        </el-form-item>
        <el-form-item
          label=""
          prop="orderId"
        >
          <el-input
            v-model="queryParams.orderId"
            placeholder="请输入订单编号"
            clearable
            @keyup.enter="handleQuery"
            style="width: 150px"
          />
        </el-form-item>
        <el-form-item
          label=""
          prop="userName"
        >
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入乘客姓名"
            clearable
            @keyup.enter="handleQuery"
            style="width: 150px"
          />
        </el-form-item>
        <el-form-item
          label=""
          prop="passengerId"
        >
          <el-input
            v-model="queryParams.passengerId"
            placeholder="请输入乘客电话"
            clearable
            @keyup.enter="handleQuery"
            style="width: 150px"
          />
        </el-form-item>
        <el-form-item
          label=""
          prop="driverId"
        >
          <el-input
            v-model="queryParams.driverId"
            placeholder="请输入司机ID"
            clearable
            @keyup.enter="handleQuery"
            style="width: 150px"
          />
        </el-form-item>
        <el-form-item
          label=""
          prop="driverId"
        >
          <el-input
            v-model="queryParams.driverId"
            placeholder="请输入司机姓名"
            clearable
            @keyup.enter="handleQuery"
            style="width: 150px"
          />
        </el-form-item>
        <el-form-item
          label=""
          prop="orderType"
          ><el-input
            v-model="queryParams.orderType"
            placeholder="请输入订单类型"
            clearable
            @keyup.enter="handleQuery"
            style="width: 150px"
        /></el-form-item>
        <el-form-item
          label=""
          prop="createTime"
        >
          <el-date-picker
            clearable
            v-model="queryParams.createTime"
            type="daterange"
            value-format="YYYY-MM-DD"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            style="width: 260px"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button
            type="info"
            icon="Search"
            @click="handleQuery()"
          />
        </el-form-item>
      </el-form>
    </div>

    <el-table
      v-loading="loading"
      :data="orderList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="订单编号"
        align="center"
        prop="orderId"
      />
      <el-table-column
        label="订单类型"
        align="center"
        prop="orderType"
      />
      <el-table-column
        label="乘客ID"
        align="center"
        prop="passengerId"
      />
      <el-table-column
        label="乘客姓名"
        align="center"
        prop="userName"
      />
      <el-table-column
        label="乘客电话"
        align="center"
        prop="phone"
      />
      <el-table-column
        label="司机ID"
        align="center"
        prop="driverId"
      />
      <el-table-column
        label="司机姓名"
        align="center"
        prop=""
      />
      <el-table-column
        label="司机电话"
        align="center"
        prop=""
      />
      <el-table-column
        label="订单价格"
        align="center"
        prop="orderPrice"
      />
      <el-table-column
        label="司机获得费"
        align="center"
        prop="driverPrice"
      />
      <el-table-column
        label="平台抽成费"
        align="center"
        prop="platformPrice"
      />
      <el-table-column
        label="出发地址"
        align="center"
        prop="startAddress"
      />
      <el-table-column
        label="目的地地址"
        align="center"
        prop="destination"
      />
      <el-table-column
        label="行驶里程/km"
        align="center"
        prop="driveMileage"
      />
      <el-table-column
        label="订单状态"
        align="center"
        prop="orderStatus"
      />
      <el-table-column
        label="取消原因"
        align="center"
        prop="cancelReason"
      />
      <el-table-column
        label="支付方式"
        align="center"
        prop="payWay"
      />
      <el-table-column
        label="出发时间"
        align="center"
        prop="startTime"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.startTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结束时间"
        align="center"
        prop="endTime"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.endTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="支付时间"
        align="center"
        prop="payTime"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.payTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="取消时间"
        align="center"
        prop="cancelTime"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.cancelTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="订单录音"
        align="center"
        prop="orderRecording"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Microphone"
            @click="platAudio"
            >点击播放</el-button
          >
          <audio
            v-show="false"
            type="audio"
            id="audio"
            :src="scope.row.orderRecording"
          ></audio>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:order:edit']"
            >详情</el-button
          >
          <el-popover
            :visible="visiblePopover"
            trigger="click"
            placement="top"
          >
            <template #reference>
              <el-button
                link
                type="primary"
                icon="Edit"
                v-hasPermi="['system:order:edit']"
                >操作</el-button
              >
            </template>
            <el-button
              link
              type="primary"
              icon="Edit"
              v-hasPermi="['system:order:edit']"
              >订单取消</el-button
            >
            <el-button
              link
              type="primary"
              icon="Edit"
              v-hasPermi="['system:order:edit']"
              >订单改价</el-button
            >
            <el-button
              link
              type="primary"
              icon="Edit"
              v-hasPermi="['system:order:edit']"
              >修改始终点</el-button
            >
            <el-button
              link
              type="primary"
              icon="Edit"
              v-hasPermi="['system:order:edit']"
              >查看轨迹</el-button
            >
          </el-popover>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      :title="title"
      v-model="openDetail"
      width="500px"
      append-to-body
    >
      <el-card>
        <template #header>
          <span>基本信息</span>
        </template>
        <el-form :inline="true">
          <el-form-item label="订单编号">
            {{ form.orderId }}
          </el-form-item>
          <el-form-item label="订单类型">
            {{ form.orderType }}
          </el-form-item>
          <el-form-item label="订单状态">
            {{ form.orderState }}
          </el-form-item>
          <el-form-item label="用户昵称">
            {{ form.nickName }}
          </el-form-item>
          <el-form-item label="支付方式">
            {{ form.payWay + " " + form.orderPrice }}
          </el-form-item>
        </el-form>
      </el-card>
      <el-card>
        <template #header>
          <span>乘客信息</span>
        </template>
        <el-form :inline="true">
          <el-form-item label="乘客姓名">
            {{ form.orderId }}
          </el-form-item>
          <el-form-item label="乘客电话">
            {{ form.orderType }}
          </el-form-item>
          <el-form-item label="乘客备注">
            {{ form.orderState }}
          </el-form-item>
        </el-form>
      </el-card>
      <el-card>
        <template #header>
          <span>司机信息</span>
          <el-form :inline="true">
            <el-form-item label="司机姓名">
              {{ form.orderId }}
            </el-form-item>
            <el-form-item label="司机电话">
              {{ form.orderType }}
            </el-form-item>
            <el-form-item label="车牌号码">
              {{ form.orderState }}
            </el-form-item>
          </el-form>
        </template>
        <el-form></el-form>
      </el-card>
      <el-card>
        <template #header>
          <span>订单信息</span>
          <el-form :inline="true">
            <el-form-item label="行驶路线">
              {{ form.orderId }}
            </el-form-item>
            <el-form-item label="出发时间">
              {{ form.orderType }}
            </el-form-item>
            <el-form-item label="结束时间">
              {{ form.orderState }}
            </el-form-item>
            <el-form-item label="取消时间">
              {{ form.nickName }}
            </el-form-item>
            <el-form-item label="支付时间">
              {{ form.payWay }}
            </el-form-item>
            <el-form-item label="取消原因">
              {{ form.payWay }}
            </el-form-item>
          </el-form>
        </template>
      </el-card>
      <el-card>
        <template #header>
          <span>费用信息</span>
          <el-form>
            <el-form-item label="订单价格">
              {{ form.orderId }}
            </el-form-item>
            <el-form-item label="明细">
              {{ form.orderType }}
            </el-form-item>
            <el-form-item label="订单改价">
              {{ form.orderState }}
            </el-form-item>
          </el-form>
        </template>
      </el-card>
      <el-card>
        <template #header>
          <span>订单操作</span>
          <el-table></el-table>
        </template>
      </el-card>
    </el-dialog>

    <!-- 添加或修改订单管理对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      width="500px"
      append-to-body
    >
      <el-form
        ref="orderRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item
          label="原订单价格"
          prop="orderPrice"
        >
          <el-input
            v-model="orderPrice"
            placeholder="请输入订单价格"
          />
        </el-form-item>
        <el-form-item
          label="改价后价格"
          prop="orderPrice"
        >
          <el-input
            v-model="form.orderPrice"
            placeholder="请输入订单价格"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button
            type="primary"
            @click="submitForm"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Order">
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
} from "@/api/system/order";

const { proxy } = getCurrentInstance();

const orderPrice = ref("");
const orderList = ref([]);
const open = ref(false);
const openDetail = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const visiblePopover = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const isActive = ref(0);
const title = ref("");
const tabs = ref([
  "全部",
  "已超时",
  "已取消",
  "呼叫中",
  "已接单",
  "司机已到达",
  "进行中",
  "待支付",
  "已完成",
  "司机待出发",
  "已退款",
]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderId: null,
    orderType: null,
    passengerId: null,
    driverId: null,
    orderRecording: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

function platAudio() {
  var audio = document.getElementById("audio");
  if (audio.paused) {
    audio.play();
  } else {
    audio.pause();
  }
}

function handleTab(index, item) {
  isActive.value = index;
  queryParams.value.orderStatus = item;
}

/** 查询订单管理列表 */
function getList() {
  loading.value = true;
  listOrder(queryParams.value).then((response) => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    orderId: null,
    orderType: null,
    passengerId: null,
    driverId: null,
    orderPrice: null,
    driverPrice: null,
    platformPrice: null,
    startAddress: null,
    destination: null,
    driveMileage: null,
    orderStatus: null,
    cancelReason: null,
    payWay: null,
    commentScore: null,
    startTime: null,
    endTime: null,
    payTime: null,
    cancelTime: null,
    createTime: null,
    updateTime: null,
    orderRecording: null,
  };
  proxy.resetForm("orderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.orderId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加订单管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _orderId = row.orderId || ids.value;
  getOrder(_orderId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改订单管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderRef"].validate((valid) => {
    if (valid) {
      if (form.value.orderId != null) {
        updateOrder(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOrder(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _orderIds = row.orderId || ids.value;
  proxy.$modal
    .confirm('是否确认删除订单管理编号为"' + _orderIds + '"的数据项？')
    .then(function () {
      return delOrder(_orderIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/order/export",
    {
      ...queryParams.value,
    },
    `order_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>

<style>
.tab {
  padding: 20px 15px 0 15px;
  background-color: #e8edf0;
  display: flex;
  border-radius: 8px 8px 0 0;
  .tab-item {
    padding: 10px;
    background-color: #d8dfe5;
    color: #a9afac;
    margin-right: 10px;
    /* margin-right: 4px; */
    border-radius: 5px 5px 0 0;
  }
  .tab-item .active {
    padding: 10px;
    color: #a9afac;
    background-color: #fff;
    margin-right: 10px;
    /* margin-right: 4px; */
    border-radius: 5px 5px 0 0;
  }
}
.header {
  padding: 20px 10px;
}
</style>
